#include "../include/main_admin.hh"
#include "../include/main_utils.hh"
#include "../include/main_customer.hh"
#include "../include/main_account.hh"
#include <limits>

// 处理管理员菜单
void handleAdminMenu(BankSystem& bankSystem, UIManager& uiManager) {
    while (true) {
        clearScreen();
        uiManager.showAdminMenu();
        
        char choice = uiManager.getKeyChoice("ABCDEF");
        
        if (choice == 'F') { // 退出
            break;
        }
        
        // 处理选择
        switch (choice) {
            case 'A': { // 用户管理
                clearScreen();
                std::cout << "===== 用户管理 =====" << std::endl << std::endl;
                
                std::cout << "请选择操作:" << std::endl;
                std::cout << "1. 查看所有客户" << std::endl;
                std::cout << "2. 添加新客户" << std::endl;
                std::cout << "3. 删除客户" << std::endl;
                std::cout << "4. 返回" << std::endl;
                
                int subChoice = uiManager.getMenuChoice(1, 4);
                
                switch (subChoice) {
                    case 1: { // 查看所有客户
                        clearScreen();
                        std::cout << "===== 所有客户列表 =====" << std::endl << std::endl;
                        
                        const auto& customers = bankSystem.getAllCustomers();
                        if (customers.empty()) {
                            std::cout << "系统中尚无客户。" << std::endl;
                        } else {
                            int i = 1;
                            for (const auto& customer : customers) {
                                std::cout << i++ << ". " << customer->getUsername() << std::endl;
                            }
                            
                            std::cout << std::endl;
                            std::string name = uiManager.getInput("请输入要查看的客户姓名 (直接回车返回): ");
                            
                            if (!name.empty()) {
                                auto customer = bankSystem.findCustomerByName(name);
                                if (customer) {
                                    // 找到客户，显示其账户详情
                                    clearScreen();
                                    customer->displayInfo();
                                    
                                    std::cout << std::endl;
                                    auto transactions = bankSystem.getTransactionsByCustomerName(name);
                                    if (!transactions.empty()) {
                                        std::cout << "最近交易记录:" << std::endl;
                                        for (size_t i = 0; i < std::min(transactions.size(), size_t(3)); ++i) {
                                            transactions[transactions.size() - 1 - i].display();
                                        }
                                    }
                                    
                                    // 添加管理选项
                                    std::cout << std::endl;
                                    std::cout << "管理选项:" << std::endl;
                                    std::cout << "1. 生成客户交易报告" << std::endl;
                                    std::cout << "2. 返回" << std::endl;
                                    
                                    int mgmtChoice = uiManager.getMenuChoice(1, 2);
                                    if (mgmtChoice == 1) {
                                        if (bankSystem.generateCustomerReport(name)) {
                                            std::cout << "已成功生成客户报告！" << std::endl;
                                        } else {
                                            std::cout << "生成报告失败。" << std::endl;
                                        }
                                    }
                                } else {
                                    std::cout << "错误：找不到客户 " << name << std::endl;
                                }
                                uiManager.getInput("按回车键继续...");
                            }
                        }
                        break;
                    }
                    case 2: { // 添加新客户
                        handleCustomerRegistration(bankSystem, uiManager);
                        break;
                    }
                    case 3: { // 删除客户
                        clearScreen();
                        std::cout << "===== 删除客户 =====" << std::endl << std::endl;
                        
                        std::string name = uiManager.getInput("请输入要删除的客户姓名: ");
                        auto customer = bankSystem.findCustomerByName(name);
                        
                        if (!customer) {
                            std::cout << "错误：客户 " << name << " 不存在。" << std::endl;
                            uiManager.getInput("按回车键继续...");
                            break;
                        }
                        
                        // 显示客户信息
                        customer->displayInfo();
                        
                        // 确认删除
                        std::string confirm = uiManager.getInput("\n警告：删除客户将同时删除其所有账户！是否继续? (Y/N): ");
                        if (toupper(confirm[0]) != 'Y') {
                            std::cout << "删除操作已取消。" << std::endl;
                            uiManager.getInput("按回车键继续...");
                            break;
                        }
                        
                        // 管理员输入密码
                        std::string adminPassword = uiManager.getInput("请输入客户密码确认删除: ");
                        
                        if (bankSystem.deleteCustomer(name, adminPassword)) {
                            std::cout << "客户 " << name << " 已成功删除。" << std::endl;
                        }
                        
                        uiManager.getInput("按回车键继续...");
                        break;
                    }
                    case 4: // 返回
                        break;
                }
                break;
            }
            case 'B': { // 查看所有交易记录
                clearScreen();
                std::cout << "===== 所有交易记录 =====" << std::endl << std::endl;
                
                const auto& transactions = bankSystem.getAllTransactions();
                if (transactions.empty()) {
                    std::cout << "系统中尚无交易记录。" << std::endl;
                } else {
                    std::cout << "请选择查看方式:" << std::endl;
                    std::cout << "1. 显示所有交易" << std::endl;
                    std::cout << "2. 生成全局交易报告" << std::endl;
                    std::cout << "3. 返回" << std::endl;
                    
                    int viewChoice = uiManager.getMenuChoice(1, 3);
                    
                    switch (viewChoice) {
                        case 1: { // 显示所有交易
                            clearScreen();
                            std::cout << "===== 所有交易记录 =====" << std::endl << std::endl;
                            
                            // 显示统计信息
                            std::cout << "总交易笔数: " << transactions.size() << std::endl << std::endl;
                            
                            // 显示所有交易
                            for (const auto& transaction : transactions) {
                                transaction.display();
                            }
                            
                            uiManager.getInput("按回车键继续...");
                            break;
                        }
                        case 2: { // 生成全局交易报告
                            if (bankSystem.generateGlobalTransactionReport()) {
                                std::cout << "已成功生成全局交易报告！" << std::endl;
                            } else {
                                std::cout << "生成报告失败。" << std::endl;
                            }
                            
                            uiManager.getInput("按回车键继续...");
                            break;
                        }
                        case 3: // 返回
                            break;
                    }
                }
                break;
            }
            case 'C': { // 生成季度报告
                clearScreen();
                std::cout << "===== 生成季度报告 =====" << std::endl << std::endl;
                
                int year, quarter;
                std::cout << "请输入年份: ";
                std::cin >> year;
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                
                std::cout << "请输入季度 (1-4): ";
                std::cin >> quarter;
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                
                if (quarter < 1 || quarter > 4) {
                    std::cout << "错误：季度必须为1到4之间的数字。" << std::endl;
                } else if (bankSystem.generateQuarterlyReport(year, quarter)) {
                    std::cout << "已成功生成季度报告！" << std::endl;
                } else {
                    std::cout << "生成报告失败。" << std::endl;
                }
                
                uiManager.getInput("按回车键继续...");
                break;
            }
            case 'D': { // 系统设置
                clearScreen();
                std::cout << "===== 系统设置 =====" << std::endl << std::endl;
                
                std::cout << "1. 语言设置" << std::endl;
                std::cout << "2. 返回" << std::endl;
                
                int settingChoice = uiManager.getMenuChoice(1, 2);
                
                if (settingChoice == 1) {
                    std::cout << std::endl;
                    std::cout << "语言设置:" << std::endl;
                    std::cout << "1. 中文" << std::endl;
                    std::cout << "2. English" << std::endl;
                    
                    int langChoice = uiManager.getMenuChoice(1, 2);
                    if (langChoice == 1) {
                        uiManager.setLanguage(Language::CHINESE);
                        std::cout << "已切换到中文界面。" << std::endl;
                    } else {
                        uiManager.setLanguage(Language::ENGLISH);
                        std::cout << "Switched to English interface." << std::endl;
                    }
                }
                
                uiManager.getInput("按回车键继续...");
                break;
            }
            case 'E': { // 账户管理
                clearScreen();
                std::cout << "===== 账户管理 =====" << std::endl << std::endl;
                
                std::cout << "请选择操作:" << std::endl;
                std::cout << "1. 查看所有账户" << std::endl;
                std::cout << "2. 删除账户" << std::endl;
                std::cout << "3. 返回" << std::endl;
                
                int accChoice = uiManager.getMenuChoice(1, 3);
                
                switch (accChoice) {
                    case 1: { // 查看所有账户
                        handleDisplayAllAccounts(bankSystem, uiManager);
                        break;
                    }
                    case 2: { // 删除账户
                        clearScreen();
                        std::cout << "===== 管理员删除账户 =====" << std::endl << std::endl;
                        
                        // 显示所有账户
                        bankSystem.displayAllAccounts();
                        std::cout << std::endl;
                        
                        std::string accountId = uiManager.getInput("请输入要删除的账户ID: ");
                        
                        auto account = bankSystem.findAccountById(accountId);
                        if (!account) {
                            std::cout << "错误：账户 " << accountId << " 不存在。" << std::endl;
                            uiManager.getInput("按回车键继续...");
                            break;
                        }
                        
                        // 确认删除
                        std::string confirm = uiManager.getInput("警告：确定要删除此账户吗? (Y/N): ");
                        if (toupper(confirm[0]) != 'Y') {
                            std::cout << "删除操作已取消。" << std::endl;
                            uiManager.getInput("按回车键继续...");
                            break;
                        }
                        
                        if (bankSystem.adminDeleteAccount(accountId)) {
                            std::cout << "账户已成功删除。" << std::endl;
                        }
                        
                        uiManager.getInput("按回车键继续...");
                        break;
                    }
                    case 3: // 返回
                        break;
                }
                break;
            }
        }
    }
} 